import Vue from 'vue'
// 创建新类避免污染全局
class FuncMain {
  constructor () {
    console.log('func by ready')
  }
  
  // 时间过滤函数 format详见日期格式
  handleDate (format, value = '') {
    let [_type, _date] = [format, new Date(value)]
    return _type
      .replace(/(y{4})/, _date.getFullYear())
      .replace(/(M{2})/, () => _date.getMonth() < 9 ? '0' + (_date.getMonth() + 1) : (_date.getMonth() + 1))
      .replace(/d{2}/, () => _date.getDate() < 10 ? '0' + (_date.getDate()) : _date.getDate())
      .replace(/H{2}/, () => _date.getHours() < 10 ? '0' + (_date.getHours()) : _date.getHours())
      .replace(/m{2}/, () => _date.getMinutes() < 10 ? '0' + (_date.getMinutes()) : _date.getMinutes())
      .replace(/s{2}/, () => _date.getSeconds() < 10 ? '0' + (_date.getSeconds()) : _date.getSeconds())
  }

  // 新增主要图标颜色
  allColor (index) {
    let arr = ['#088BEB', '#0EC49E', '#F0BD26', '#FF604D', '#60C9FF', '#1E6498', '#00A8EE', '#00DCDE', '#FF72A4', '#FFE400']
    if (typeof index == typeof 1) return arr[index] || ''
    return arr
  }

  // 快速排序 (倒叙)(只可排序数组内对象)
  quickSort(arr, key){
    if (arr.length <= 1) return arr
    let [newArr, left, right] = [arr.splice(Math.floor(arr.length / 2), 1), [], []]
    for (let item of arr) item[key] < newArr[0][key] ? left.push(item) : right.push(item)

    return [...this.quickSort(right, key), ...newArr, ...this.quickSort(left, key)]
  }

  // 向前获取时间
  forwardDate (reverse, currentDate = new Date()) {
    let [date, dateArr] = [currentDate, []]
    dateArr.push(this.handleDate('MM-dd', date))

    for (let i = 1 ; i < 31 ; i++) {
      let day = new Date(date.getTime() - (86400000 * i))
      if (day.getDate() == date.getDate()) break
      dateArr.push(this.handleDate('MM-dd', day))
    }

    return reverse ? dateArr.reverse() : dateArr
  }

  // 新增vue过滤器
  addFilter (filterName, filterObj) {
    Vue.filter(filterName, value => {
      return filterObj[value] || '-'
    })
  }

  // 新增方法
  addfunc (funcName, func) {
    this[funcName] = func
  }
}

// 暴露接口
// let funcMains = new FuncMain()

export default new FuncMain()